<?php include "../public/header.php";?>
<?php include "../public/header_edit.php";?>
<style>
    #commentForm .main-container xm-select{border: 0 !important;}
</style>
<body id="optPage">
<form class="layui-form layui-form-pane" id="commentForm">
    <div class="page-title"><div class="page-title-con">
            <span>新建证书</span><div class="move_title"></div><?php include "../public/sub.php";?></div>
    </div>
    <div class="main-container">
        <div class="block-first">
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label"><text>*</text>人员姓名</label>
                        <div class="layui-input-block">
                            <input type="text" readonly name="personid" class="layui-input personid" placeholder="请选择" onclick="openPage('../public/choose_person.php?dom=personid&page=cert','80%','80%')">
                            <div class="choose_div" id="personid" key="1"></div>
                            <span class="choose_one" onclick="openPage('../public/choose_person.php?dom=personid&page=cert','80%','80%')">
                                <i class="layui-icon layui-icon-search"></i>
                            </span>
                        </div>
                    </div>
                </div>
            </div>

            <div class="layui-form-item">
                <div class="short_table_list" key="1">
                    <div class="opt-btn-group">
                        <div><span>证书明细<span style="margin-left: 20px;color: red;font-size: 12px;">①.如有多专业，请用|隔开，范例：市政公用工程|公路工程|建筑工程 ②.有效期仅为主项的有效期，增项专业的有效期需到证书库编辑。</span></span></div>
                        <div>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_add_row">插入行</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_del_row">删除行</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_del_all">删除全部</button>
                        </div>
                    </div>
                    <table class="layui-table" lay-size="sm">
                        <thead class="thead">
                        <tr>
                            <th class="width35">序号</th>
                            <th><text>*</text>证书分类</th>
                            <th><text>*</text>证书名称</th>
                            <th>证书编号</th>
                            <th>发证单位</th>
                            <th>注册/发证日期</th>
                            <th>专业</th>
                            <th><text>!</text>有效期止</th>
                            <th><text>*</text>证书形式</th>
                            <th><text>*</text>证书状态</th>
                            <th><text>!</text>电子件有效期</th>
                            <th><text>!</text>下次年检时间</th>
                            <th>保管人</th>
                            <th>存放地</th>
                            <th>备注</th>
                            <th>附件</th>
                        </tr>
                        </thead>
                        <tbody class="short_table_tbody" id="list_list" style="overflow-y: scroll;">
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</form>
<script>
    function choose_custodianid(obj){
        var index = $(obj).parents('tr').find('td:first-child').text();
        index = parseInt(index) - 1;
        openPage('../public/choose_userpart.php?dom=custodianid&index='+index+'&page=mulcert','770px','490px');
    }

    $('.short_table_add_row').click(function () {
        setTimeout(function () {
            if($('#list_list tr').length > 0) {
                $('.thead').css('overflow-y','scroll');
            }
        }, 100)
    });


    function short_table_opt_func(bridge_str, table_key, xmObj=[]){
        if(!table_key || table_key == undefined || table_key == 'undefined'){
            table_key = 1;
        }
        update_refresh_row_func();

        $(".short_table_add_row").click(function () {
            if($(this).parents('.short_table_list').attr('key') != table_key){
                return false;
            }

            var sle = false;
            var sle_obj;
            $.each($(this).parents('.short_table_list').find('table').children('tbody').children('tr'),function () {
                if($(this).attr('sle') == 'sle'){
                    sle = true;
                    sle_obj = $(this);
                }
            });

            if(!sle){
                $(this).parents('.short_table_list').find('table').children('tbody').append(bridge_str);
            }else{
                $(sle_obj).after(bridge_str);
            }

            $.each($(this).parents('.short_table_list').find('table').children('tbody').children('tr'),function (key,val) {
                $(this).children('td:first-child').html(key+1);
            });

            // 设置滚动条
            if($(this).parents('.short_table_list').find('table').children('tbody').height() >= 500){
                $(this).parents('.short_table_list').find('table').children('thead').css('overflow-y','scroll')
            }

            render_date();
            render_from();
            update_refresh_row_func();
            set_list_render_xmselect(xmObj);
            reload_table_list_bac('short_table_list');
        });

        $(".short_table_copy_row").click(function () {
            if($(this).parents('.short_table_list').attr('key') != table_key){
                return false;
            }
            var sle = false;
            var sle_obj,sle_html;
            $.each($(this).parents('.short_table_list').find('table').children('tbody').children('tr'),function () {
                if($(this).attr('sle') == 'sle'){
                    sle = true;
                    sle_obj = $(this);
                    sle_html = '<tr>'+$(this).formhtml()+'</tr>';

                    if($(this).find('.filedetaobjdata').html() != undefined){
                        var filedetaobjdataStr = $(this).find('.filedetaobjdata').html();
                        var filedetaobjdataLength = $(this).find('.filedetaobjdata').parent('div').find('span:first-child').html();
                        if(filedetaobjdataLength > 0){
                            $(this).find('.filedetaobjdata').parent('div').find('span:first-child').html(0);
                        }
                        $(this).find('.filedetaobjdata').html('[]');
                        sle_html = '<tr>'+$(this).formhtml()+'</tr>';
                        if(filedetaobjdataLength > 0){
                            $(this).find('.filedetaobjdata').parent('div').find('span:first-child').html(filedetaobjdataLength);
                        }
                        $(this).find('.filedetaobjdata').html(filedetaobjdataStr);
                    }
                }
            });

            if(!sle){
                layer.msg("请选中要复制的行");return false;
            }else{
                $(sle_obj).after(sle_html);
            }
            $.each($(this).parents('.short_table_list').find('table').children('tbody').children('tr'),function (key,val) {
                $(this).children('td:first-child').html(key+1);
            });

            // 设置滚动条
            if($(this).parents('.short_table_list').find('tbody').height() >= 500){
                $(this).parents('.short_table_list').find('thead').css('overflow-y','scroll')
            }

            render_date();
            render_from();
            update_refresh_row_func();
            set_list_render_xmselect(xmObj);
            reload_table_list_bac('short_table_list');
        });

        $(".short_table_del_row").click(function () {
            if($(this).parents('.short_table_list').attr('key') != table_key){
                return false;
            }
            var sle = false;
            var sle_obj;
            $.each($(this).parents('.short_table_list').find('table').children('tbody').children('tr'),function () {
                // 样式处理
                $(this).find('td').css({'border': '1px solid #eee'});
                $('.layui-table thead th ').css({'border-bottom-color': '#eee'});

                if($(this).attr('sle') == 'sle'){
                    sle = true;
                    sle_obj = $(this);
                }
            });

            if(!sle){
                layer.msg("请选中要删除的行");return false;
            }else{
                $(sle_obj).remove();
            }

            // if($(this).parents('.short_table_list').find('table').children('tbody').children('tr').length < 1){
            //     $(this).parents('.short_table_list').children('table').children('tbody').append(bridge_str);
            // }

            $.each($(this).parents('.short_table_list').find('table').children('tbody').children('tr'),function (key,val) {
                $(this).children('td:first-child').html(key+1);
            });

            // 取消滚动条
            if($(this).parents('.short_table_list').find('tbody').height() < 500){
                $(this).parents('.short_table_list').find('thead').css('overflow-y','hidden')
            }

            render_date();
            render_from();
            update_refresh_row_func();
            set_list_render_xmselect(xmObj);
            reload_table_list_bac('short_table_list');
        });

        $(".short_table_del_all").click(function () {
            if($(this).parents('.short_table_list').attr('key') != table_key){
                return false;
            }
            var sle = false;
            var sle_obj;
            $(this).parents('.short_table_list').find('table').children('tbody').html('');

            // $(this).parents('.short_table_list').find('table').children('tbody').append(bridge_str);
            $.each($(this).parents('.short_table_list').find('table').children('tbody').children('tr'),function (key,val) {
                $(this).children('td:first-child').html(key+1);
            });

            // 取消滚动条
            if($(this).parents('.short_table_list').find('tbody').height() < 500){
                $(this).parents('.short_table_list').find('thead').css('overflow-y','hidden')
            }

            render_date();
            render_from();
            update_refresh_row_func();
            set_list_render_xmselect(xmObj);
            reload_table_list_bac('short_table_list');
        });
    }

    function set_list_render_xmselect(xmArr) {
        if(xmArr){
            for(let i=0;i<xmArr.length;i++){
                let xmObj = xmArr[i];

                if(xmObj && xmObj.name){
                    var types = 'dan';
                    if(xmObj.type && xmObj.type == 'duo'){
                        types = 'duo';
                    }

                    $('.'+xmObj.name).each(function (i) {
                        if($(this).attr('class') == xmObj.name){
                            var num = get_rand_xm_classname(xmObj.name);
                            $(this).addClass(xmObj.name+num).attr('key',num);
                            if(types == 'dan'){
                                xmselect_list_dan(xmObj.name+num, xmObj.data, '');
                            }else{
                                xmselect_list_duo(xmObj.name+num, xmObj.data, []);
                            }
                        }else{
                            var key = $(this).attr('key');
                            if($("."+xmObj.name+key).length > 1){
                                var other = $("."+xmObj.name+key)[1];
                                var xmValue = $(other).find('input').val() || '';
                                $(other).html('');
                                var num = get_rand_xm_classname(xmObj.name);
                                $(other).attr('class', xmObj.name+' '+xmObj.name+num).attr('key',num);
                                if(types == 'dan'){
                                    xmselect_list_dan(xmObj.name+num, xmObj.data, xmValue);
                                }else{
                                    xmselect_list_duo(xmObj.name+num, xmObj.data, xmValue.split(","));
                                }
                            }
                        }
                    });
                }
            }
        }
    }


    layui.use(['laytp', 'form'], function() {
        var form = layui.form;
        render_input_attr();

        facade.ajax({type: 'get', url: ask_cert_muladd}).done(function(res){
            if (res.code === 2000) {
                var form = res.data.form;
                var form_str = '<option value=""></option>';
                for (var i in form){
                    form_str += '<option value="'+i+'">'+form[i]+'</option>';
                }
                $('.form').html(form_str);

                var state = res.data.state;
                var state_str = '';
                for (var i in state){
                    state_str += '<option value="'+i+'">'+state[i]+'</option>';
                }
                $('.state').html(state_str);

                var str = '';
                str += '<tr>';
                str += '<td class="width35">0</td>';
                str += '<td><div class="type"></div></td>';
                str += '<td><input type="text" class="layui-input name"></td>';
                str += '<td><input type="text" class="layui-input numberss"></td>';
                str += '<td><input type="text" class="layui-input issue_unit"></td>';
                str += '<td><input type="text" class="layui-input issue_dt bdate_ymd"></td>';
                str += '<td><input type="text" class="layui-input major"></td>';
                str += '<td><input type="text" class="layui-input expiry_dt bdate_ymd"></td>';
                str += '<td><select class="layui-select form">';
                for (var i in form){
                    str += '<option value="'+i+'">'+form[i]+'</option>';
                }
                str += '</select></td>';
                str += '<td><select class="layui-select state">';
                for (var i in state){
                    str += '<option value="'+i+'">'+state[i]+'</option>';
                }
                str += '</select></td>';
                str += '<td><input type="text" class="layui-input electron_dt bdate_ymd"></td>';
                str += '<td><input type="text" class="layui-input inspect_dt bdate_ymd"></td>';
                str += '<td>';
                str += '<div class="tanxian" style="border: 0;">';
                str += '<div class="tanxianclick" onclick="choose_custodianid(this)"></div>';
                str += '<div class="tanxiannei custodianid" id="custodianid"></div>';
                str += '</div>';
                // str += '<div class="tancheckdiv"><div class="tancheck" onclick="choose_custodianid(this)"><i class="layui-icon layui-icon-search"></i></div></div>';
                str += '<div id="custodianid_tanerror" class="tanerrormsg"></div>';
                str += '</td>';
                str += '<td><div class="store_addr"></div></td>';
                str += '<td><input type="text" class="layui-input explain"></td>';
                str += '<td>'+public_show_file_deta('list_list', [],1)+'</td>';
                str += '</tr>';
                short_table_opt_func(str, 1, [{name:'type', data:res.data.type, value:''},{name:'store_addr', data:res.data.store_addr, value:''}]);

                // public_upload_file_load('upload_files');
                render_from();
            }
        });

        form.on('submit(add)', function(data){

            data.field.personid = get_choose_val('personid');


            var validRes = $("#commentForm").valid();
            formValid('personid');
            if(data.field.personid == undefined || !data.field.personid){
                validRes = false;
            }
            if(!validRes){return false;}


            var list = [''];
            try {
                $('#list_list tr').each(function (i) {
                    let type = $(this).find('.type').find('input').val();
                    let name = $(this).find('.name').val();
                    let number = $(this).find('.numberss').val();
                    let issue_unit = $(this).find('.issue_unit').val();
                    let major = $(this).find('.major').val();
                    let issue_dt = $(this).find('.issue_dt').val();
                    let expiry_dt = $(this).find('.expiry_dt').val();
                    let form = $(this).find('.form').val();
                    let state = $(this).find('.state').val();
                    let electron_dt = $(this).find('.electron_dt').val();
                    let inspect_dt = $(this).find('.inspect_dt').val();
                    let custodianid = $(this).find('#custodianid>div>div').attr('data-id');
                    let store_addr = $(this).find('.store_addr').find('input').val();
                    let explain = $(this).find('.explain').val();
                    let files = public_deta_file_value(this);
                    var index = i + 1;

                    if(!type){
                        throw new Error('第'+index+'行，请选择证书分类');
                    }
                    if(!name){
                        throw new Error('第'+index+'行，请填写证书名称');
                    }
                    list.push({
                        type:type,
                        name: name,
                        number:number,
                        issue_unit:issue_unit,
                        major:major,
                        issue_dt:issue_dt,
                        expiry_dt:expiry_dt,
                        form:form,
                        state:state,
                        electron_dt:electron_dt,
                        inspect_dt:inspect_dt,
                        custodianid:custodianid,
                        store_addr:store_addr,
                        explain:explain,
                        files:files
                    });
                });
            } catch(e) {
                $(".listerror").find('span').text(e.message);
                $(".listerror").show();
                return false;
            }
            $(".listerror").hide();
            data.field.list = list;

            facade.ajax({url: ask_cert_muladd, data:data.field}).done(function(res){
                if (res.code === 2000) {
                    dataPostTimeOut();
                }
            });
            return false;
        });
    });
</script>
</body>
</html>
